<?php
/**
* 多主题 配置数据处理
*
* MultiThemes for Z-BlogPHP
*
* @author      心扬<chrisyoungpark@qq.com>
* @version     1.0
*/

header('Content-Type: application/json');
header('Content-Type: text/html;charset=utf-8');

require __DIR__ . '/../../../zb_system/function/c_system_base.php';
require __DIR__ . '/../../../zb_system/function/c_system_admin.php';

$zbp->Load();
$action = 'root';
if (!$zbp->CheckRights($action)) {
    echo JSON_msg_return(false, '没有访问权限!');
    die();
}
if (!$zbp->CheckPlugin('MultiThemes')) {
    echo JSON_msg_return(false, '插件未启用!');
    die();
}

// 配置项
$clients = $zbp->Config('MultiThemes')->clients;

//配置操作
$action = $_GET['action'];//操作类别

if ($action == 'get') { //获取主题
    $client_id = $_GET['cid'];
    $themes_info = array();
    
    foreach (GetDirsInDir($zbp->usersdir . 'theme/') as $key => $value) {
        $app = new App;
        $app->LoadInfoByXml('theme', $value);
        $themes_info[$key]['id']   = $app->id;
        $themes_info[$key]['name'] = urlencode($app->name); //主题名
        $themes_info[$key]['img']  = urlencode($app->GetScreenshot()); //截屏

        foreach ($app->GetCssFiles() as $k => $v) //css
            $themes_info[$key]['css'][] = $k;

        if ($app->id == $clients[$client_id][0]) {
            $themes_info[$key]['actived'] = 1;
            $themes_info[$key]["style"] = $clients[$client_id][1];
        } else {
            $themes_info[$key]['actived'] = 0;
            $themes_info[$key]["style"] = '';
        }
    }

    echo urldecode(json_encode($themes_info));
} elseif($action == 'set') { //设置主题
    $client_id = $_GET['cid'];
    $clients[$client_id][0] = $_GET['theme'];
    $clients[$client_id][1] = $_GET['style'];

    $zbp->Config('MultiThemes')->clients = $clients;
    $zbp->SaveConfig('MultiThemes');

    $set_result = BuildTemplate_MuitiTheme($clients[$client_id][0], $clients[$client_id][1]);

    if ($set_result) 
        echo JSON_msg_return(true, '主题设置成功！');
    else
        echo JSON_msg_return(false, '主题编译失败!');
} elseif ($action == 'config') { //插件配置
    $fields = json_decode($_GET['fields']);
    $config = $zbp->Config('MultiThemes')->config;
    if ($fields->thismode) {
        $zbp->Config('MultiThemes')->config = array(
            $fields->mode, //是否启用客户端UserAgent检测
            isset($fields->dand)?true:false, //是否启用安卓Android客户端检测
            isset($fields->dios)?true:false  //是否启用苹果iOS客户端检测
        );
    } else {
        $zbp->Config('MultiThemes')->tabname = array(
            $fields->tabname0,
            $fields->tabname1,
            $fields->tabname2,
            $fields->tabname3,
            $fields->tabname4
        );

        $zbp->Config('MultiThemes')->config = array(
            $fields->mode, //是否启用客户端UserAgent检测
            $config[1], //不用设置
            $config[2]  //不用设置
        );
    }

    if ($zbp->SaveConfig('MultiThemes')) 
        echo JSON_msg_return(true, '设置保存成功！');
    else 
        echo JSON_msg_return(true, '设置保存失败！');
}

/**
 * 编译模板
 * @return bool
 */
function BuildTemplate_MuitiTheme($theme, $style) 
{
    global $zbp;

    if (file_exists($zbp->usersdir . 'cache/compiled/'. $theme . '/')) {
        return true;
    } else {
        $zbp->template = $zbp->PrepareTemplate($theme);
        $zbp->BuildTemplate();

        if (file_exists($zbp->template->path))
            return true;
        else
            return false;
    }
}

/**
 * 返回JSON消息
 * @return string
 */
function JSON_msg_return($status, $msg)
{
    $arr = array(urlencode($status), urlencode($msg));

    return urldecode(json_encode($arr));
}
?>
